#include "gs_Main.h"
#include <cstdlib>

//card loading
char gs_Main::classicCards[][40] = {
        "data/classic/back.png",
        "data/classic/+4.png",
        "data/classic/jok.png",
        "data/classic/+2-blue.png",
        "data/classic/pas-blue.png",
        "data/classic/inv-blue.png",
        "data/classic/0-blue.png",
        "data/classic/1-blue.png",
        "data/classic/2-blue.png",
        "data/classic/3-blue.png",
        "data/classic/4-blue.png",
        "data/classic/5-blue.png",
        "data/classic/6-blue.png",
        "data/classic/7-blue.png",
        "data/classic/8-blue.png",
        "data/classic/9-blue.png",
         "data/classic/+2-green.png",
        "data/classic/pas-green.png",
        "data/classic/inv-green.png",
        "data/classic/0-green.png",
        "data/classic/1-green.png",
        "data/classic/2-green.png",
        "data/classic/3-green.png",
        "data/classic/4-green.png",
        "data/classic/5-green.png",
        "data/classic/6-green.png",
        "data/classic/7-green.png",
        "data/classic/8-green.png",
        "data/classic/9-green.png",
        "data/classic/+2-red.png",
        "data/classic/pas-red.png",
        "data/classic/inv-red.png",
        "data/classic/0-red.png",
         "data/classic/1-red.png",
        "data/classic/2-red.png",
        "data/classic/3-red.png",
        "data/classic/4-red.png",
        "data/classic/5-red.png",
        "data/classic/6-red.png",
        "data/classic/7-red.png",
        "data/classic/8-red.png",
        "data/classic/9-red.png",
        "data/classic/+2-yellow.png",
        "data/classic/pas-yellow.png",
        "data/classic/inv-yellow.png",
        "data/classic/0-yellow.png",
        "data/classic/1-yellow.png",
        "data/classic/2-yellow.png",
         "data/classic/3-yellow.png",
         "data/classic/4-yellow.png",
        "data/classic/5-yellow.png",
        "data/classic/6-yellow.png",
        "data/classic/7-yellow.png",
        "data/classic/8-yellow.png",
        "data/classic/9-yellow.png"
};


gs_Main::gs_Main(B_GameStateHandler* handler) : B_GameState(handler)
{
    //load a good font to display
	m_env->getSkin()->setFont(m_env->getFont("data/font.bmp"));

	m_message = m_env->addStaticText(L"SPACE: Change cam view\nESC: Exit",
                                        irr::core::rect<int>(10,10,200,42), true);

    m_cam = m_smgr->addCameraSceneNode(0,
          irr::core::vector3df(0,50,-70),
          irr::core::vector3df(0,0,0));
    //m_cam->setPosition(irr::core::vector3df(0,50,-70));
    //m_cam->setTarget(irr::core::vector3df(0,0,0));


    //m_player

    irr::scene::IBillboardSceneNode *bill;
    for(int i=0;i<5;i++)
    {
        bill = m_smgr->addBillboardSceneNode(0,
            irr::core::dimension2d<irr::f32>(10.0,15.0)
        );
        bill->setMaterialFlag(irr::video::EMF_LIGHTING, false);
        bill->setMaterialType(irr::video::EMT_SOLID);
        bill->setMaterialTexture(0, m_driver->getTexture(classicCards[rand()%56]));
        bill->setPosition(irr::core::vector3df(0,0,-40));
        m_player.push_back(bill);
    }

    //m_player1
    for(int i=0;i<5;i++)
    {
        bill = m_smgr->addBillboardSceneNode(0,
            irr::core::dimension2d<irr::f32>(10.0,15.0)
        );
        bill->setMaterialFlag(irr::video::EMF_LIGHTING, false);
        bill->setMaterialType(irr::video::EMT_SOLID);
        bill->setMaterialTexture(0, m_driver->getTexture("data/classic/back.png"));
        bill->setPosition(irr::core::vector3df(0,0,40));
        m_player1.push_back(bill);
    }

    //m_player2
    for(int i=0;i<5;i++)
    {
        bill = m_smgr->addBillboardSceneNode(0,
            irr::core::dimension2d<irr::f32>(10.0,15.0)
        );
        bill->setMaterialFlag(irr::video::EMF_LIGHTING, false);
        bill->setMaterialType(irr::video::EMT_SOLID);
        bill->setMaterialTexture(0, m_driver->getTexture("data/classic/back.png"));
        bill->setPosition(irr::core::vector3df(40,0,0));
        m_player2.push_back(bill);
    }
    //m_player3
    for(int i=0;i<5;i++)
    {
        bill = m_smgr->addBillboardSceneNode(0,
            irr::core::dimension2d<irr::f32>(10.0,15.0)
        );
        bill->setMaterialFlag(irr::video::EMF_LIGHTING, false);
        bill->setMaterialType(irr::video::EMT_SOLID);
        bill->setMaterialTexture(0, m_driver->getTexture("data/classic/back.png"));
        bill->setPosition(irr::core::vector3df(-40,0,0));
        m_player3.push_back(bill);
    }
	//deck position and texture
	m_deck = m_smgr->addBillboardSceneNode(0,
        irr::core::dimension2d<irr::f32>(10.0,15.0)
    );

    m_deck->setMaterialFlag(irr::video::EMF_LIGHTING, false);
    m_deck->setMaterialTexture(0, m_driver->getTexture("data/classic/back.png"));
	m_deck->setMaterialTexture(1, m_driver->getTexture("data/water.jpg"));
	m_deck->setMaterialType(irr::video::EMT_REFLECTION_2_LAYER);
	m_deck->setPosition(irr::core::vector3df(0,40,0));


    //set up center/top cards
    m_playerTop = m_smgr->addBillboardSceneNode(0,
        irr::core::dimension2d<irr::f32>(15.0,22.5)
    );
    m_playerTop->setMaterialFlag(irr::video::EMF_LIGHTING, false);
	m_playerTop->setMaterialType(irr::video::EMT_SOLID);
	m_playerTop->setMaterialTexture(0, m_driver->getTexture("data/classic/+2-red.png"));
	m_playerTop->setPosition(irr::core::vector3df(0,0,-5));

	m_player1Top = m_smgr->addBillboardSceneNode(0,
        irr::core::dimension2d<irr::f32>(15.0,22.5)
    );
    m_player1Top->setMaterialFlag(irr::video::EMF_LIGHTING, false);
	m_player1Top->setMaterialType(irr::video::EMT_SOLID);
	m_player1Top->setMaterialTexture(0, m_driver->getTexture("data/classic/+2-green.png"));
	m_player1Top->setPosition(irr::core::vector3df(0,0,5));


	m_player2Top = m_smgr->addBillboardSceneNode(0,
        irr::core::dimension2d<irr::f32>(15.0,22.5)
    );
    m_player2Top->setMaterialFlag(irr::video::EMF_LIGHTING, false);
	m_player2Top->setMaterialType(irr::video::EMT_SOLID);
	m_player2Top->setMaterialTexture(0, m_driver->getTexture("data/classic/+2-yellow.png"));
	m_player2Top->setPosition(irr::core::vector3df(5,0,0));


	m_player3Top = m_smgr->addBillboardSceneNode(0,
        irr::core::dimension2d<irr::f32>(15.0,22.5)
    );
    m_player3Top->setMaterialFlag(irr::video::EMF_LIGHTING, false);
	m_player3Top->setMaterialType(irr::video::EMT_SOLID);
	m_player3Top->setMaterialTexture(0, m_driver->getTexture("data/classic/+2-blue.png"));
	m_player3Top->setPosition(irr::core::vector3df(-5,0,0));

    m_highlight=NULL;
}

gs_Main::~gs_Main(void)
{
	if(m_message) m_message->remove();
	if(m_cam) m_cam->remove();

	while(m_player.size())
    {
        m_player[0]->remove();
        m_player.erase(0);
    }
    while(m_player1.size())
    {
        m_player1[0]->remove();
        m_player1.erase(0);
    }
    while(m_player2.size())
    {
        m_player2[0]->remove();
        m_player2.erase(0);
    }
    while(m_player3.size())
    {
        m_player3[0]->remove();
        m_player3.erase(0);
    }
    if(m_deck) m_deck->remove();
    if(m_playerTop) m_playerTop->remove();
    if(m_player1Top) m_player1Top->remove();
    if(m_player2Top) m_player2Top->remove();
    if(m_player3Top) m_player3Top->remove();
}

void gs_Main::Render() {

	m_driver->beginScene(true, true, irr::video::SColor(0,0,50,0) );
    m_smgr->drawAll();
    m_env->drawAll();

    //highlighting
    irr::core::position2d<irr::s32> m = m_device->getCursorControl()->getPosition();
    irr::scene::ISceneNode *coll = m_smgr->getSceneCollisionManager()->getSceneNodeFromScreenCoordinatesBB(m);

    //recalculate all positions for each deck
    //m_player 0,0,-40
    int n;
    float z;

    n=m_player.size();
    bool gotHighlight=false;//if pointing at empy space make all cards same size
    for(int i=0;i<n;i++)
    {
        irr::core::vector3df pos;
        if(n<10)
            pos=irr::core::vector3df(((n-1)*5.0)-(i*10),0,-40);
        else if(n<15)
            pos=irr::core::vector3df(((n-1)*3.5f)-(i*7),0,-40);
        else
            pos=irr::core::vector3df(((n-1)*2.5f)-(i*5),0,-40);
        m_player[i]->setPosition(pos);
        if(m_player[i]==coll)
        {
            gotHighlight=true;
            if(m_highlight)m_highlight->setSize(irr::core::dimension2df(10.0f,15.0f));
            m_highlight=((irr::scene::IBillboardSceneNode*)m_player[i]);
            m_highlight->setSize(irr::core::dimension2df(15.0f,22.5f));
            m_highlight->setPosition(m_highlight->getPosition()+irr::core::vector3df(0,0,-1));
        }
    }
    if(!gotHighlight)
    {
        if(m_highlight)m_highlight->setSize(irr::core::dimension2df(10.0f,15.0f));
        m_highlight=NULL;
    }
    //m_player1
    n=m_player1.size();
    for(int i=0;i<n;i++)
    {
        irr::core::vector3df pos;
        if(n<10)
            pos=irr::core::vector3df(((n-1)*5.0)-(i*10),0,40);
        else if(n<15)
            pos=irr::core::vector3df(((n-1)*3.5f)-(i*7),0,40);
        else
            pos=irr::core::vector3df(((n-1)*2.5f)-(i*5),0,40);
        m_player1[i]->setPosition(pos);
    }
    //m_player2
    n=m_player2.size();
    for(int i=0;i<n;i++)
    {
        irr::core::vector3df pos;
        if(n<10)
            pos=irr::core::vector3df(40,0,((n-1)*5.0)-(i*10));
        else if(n<15)
            pos=irr::core::vector3df(40,0,((n-1)*3.5f)-(i*7));
        else
            pos=irr::core::vector3df(40,0,((n-1)*2.5f)-(i*5));
        m_player2[i]->setPosition(pos);
    }
    //m_player3
    n=m_player3.size();
    for(int i=0;i<n;i++)
    {
        irr::core::vector3df pos;
        if(n<10)
            pos=irr::core::vector3df(-40,0,((n-1)*5.0)-(i*10));
        else if(n<15)
            pos=irr::core::vector3df(-40,0,((n-1)*3.5f)-(i*7));
        else
            pos=irr::core::vector3df(-40,0,((n-1)*2.5f)-(i*5));
        m_player3[i]->setPosition(pos);
    }

    //check if deck is highlited
    if(coll==m_deck)
        m_highlightDeck = (irr::scene::IBillboardSceneNode*)coll;
    else if(m_highlightDeck!=NULL)
        m_highlightDeck = NULL;

	m_driver->endScene();
}

bool gs_Main::onEvent(irr::SEvent event) {

	if( event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown ) {
		if( event.KeyInput.Key == irr::KEY_ESCAPE ) {
			m_handler->RequestStateChange("GS_MENU");
			return true;
		}
		if( event.KeyInput.Key == irr::KEY_SPACE ) {
            //calc which side to turn add animator and animate cam
            irr::core::vector3df p=m_cam->getPosition();
            p.rotateXZBy(90.0f,irr::core::vector3df(0,50,0));
            irr::scene::ISceneNodeAnimator *anim = m_smgr->createFlyStraightAnimator(m_cam->getPosition(),p,1000,false);
            m_cam->addAnimator(anim);
            anim->drop();
		    return true;
		}
	}
	if( event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event  == irr::EMIE_LMOUSE_PRESSED_DOWN)
	{
	    if(m_highlight != NULL)
	    {
	        //has clicked on highlighted node only
	        //delete prev center node then add new node
	        irr::scene::ISceneNodeAnimator* anim = m_smgr->createDeleteAnimator(1000);
	        m_playerTop->addAnimator(anim);
	        anim->drop();

            anim = m_smgr->createFlyStraightAnimator(m_highlight->getAbsolutePosition(),
                    irr::core::vector3df(0,0,-10),1000,false);
            m_highlight->addAnimator(anim);
            anim->drop();

            for(int i=0;i<m_player.size();i++)
            {
                if(m_player[i]==m_highlight)
                {
                    m_player.erase(i);
                    break;
                }
            }
            m_playerTop = m_highlight;
            ((irr::scene::IBillboardSceneNode*)m_playerTop)->setSize(irr::core::dimension2df(15.0,22.5));
            m_highlight = NULL;
	    }
	    if(m_highlightDeck != NULL )
	    {
	        //get one card ,for now onto players stack
            int rnd = rand()%56;
	        irr::scene::IBillboardSceneNode *bill = m_smgr->addBillboardSceneNode(0,
            irr::core::dimension2d<irr::f32>(10.0,15.0)
            );
            bill->setMaterialFlag(irr::video::EMF_LIGHTING, false);
            bill->setMaterialType(irr::video::EMT_SOLID);
            bill->setMaterialTexture(0, m_driver->getTexture(classicCards[rnd]));
            bill->setPosition(irr::core::vector3df(1000,1000,-40));
            m_player.push_back(bill);

            //create a new animated node showing draw of card
            irr::scene::IBillboardSceneNode *billAnim = m_smgr->addBillboardSceneNode(0,
            irr::core::dimension2d<irr::f32>(10.0,15.0)
            );
            billAnim->setMaterialFlag(irr::video::EMF_LIGHTING, false);
            billAnim->setMaterialType(irr::video::EMT_SOLID);
            billAnim->setMaterialTexture(0, m_driver->getTexture(classicCards[rnd]));
            billAnim->setPosition(irr::core::vector3df(0,50,0));
            irr::core::vector3df pos;
            int n=m_player.size();
            if(n<10)
                pos=irr::core::vector3df(((n-1)*-5.0),0,-40);
            else if(n<15)
                pos=irr::core::vector3df(((n-1)*-3.5f),0,-40);
            else
                pos=irr::core::vector3df(((n-1)*-2.5f),0,-40);
            irr::scene::ISceneNodeAnimator* anim = m_smgr->createFlyStraightAnimator(m_deck->getPosition(),
                pos,1000,false);
            billAnim->addAnimator(anim);
            anim->drop();

            anim=m_smgr->createDeleteAnimator(1000);
            billAnim->addAnimator(anim);
            anim->drop();

	    }
	}
    return false;
}

void gs_Main::Update() {
	Render();
}
